package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.*;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import com.atguigu.jxc.util.BigDecimalUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class PurchaseListGoodsServiceImpl implements PurchaseListGoodsService {


    @Autowired
    private UserDao userDao;

    @Autowired
    private PurchaseListGoodsDao purchaseListGoodsDao;

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private GoodsTypeDao goodsTypeDao;

    @Autowired
    private PurchaseListDao purchaseListDao;

    @Autowired
    private GoodsService goodsService;


    /**
     * 保存进货单信息
     * @param purchaseList 进货单信息实体
     * @param purchaseListGoodsStr 进货商品信息JSON字符串
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(PurchaseList purchaseList, String purchaseListGoodsStr, HttpSession session) {
        User user = (User) session.getAttribute("currentUser");
        Integer userId = user.getUserId();
        purchaseList.setUserId(userId);
        Gson gson = new Gson();
        Type type = new TypeToken<List<PurchaseListGoods>>() {}.getType();
        List<PurchaseListGoods> purchaseListGoodsList = gson.fromJson(purchaseListGoodsStr, type);
        //保存进货单
        purchaseListDao.insertPurchaseList(purchaseList);
        //增加入库信息
        for (PurchaseListGoods purchaseListGoods : purchaseListGoodsList) {
            purchaseListGoods.setPurchaseListId(purchaseList.getPurchaseListId());
            purchaseListGoodsDao.savePurchaseListGoods(purchaseListGoods);
            goodsService.updateGoodsStock(purchaseListGoods.getGoodsId(),
                    purchaseListGoods.getGoodsNum(),
                    purchaseListGoods.getPrice(),
                    1,
                    0);
        }
    }

    /**
     * 查询进货单
     * @param purchaseNumber 单号
     * @param supplierId 供应商ID
     * @param state 付款状态
     * @param sTime 开始时间
     * @param eTime 结束时间
     * @return
     */
    @Override
    public Map<String, Object> list(String purchaseNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        Map<String,Object> result = new HashMap<>();
        List<PurchaseList> purchaseListList = purchaseListDao.getPurchaseList(purchaseNumber, supplierId, state, sTime, eTime);
        result.put("rows", purchaseListList);
        return result;
    }

    /**
     * 查询进货单商品信息
     * @param purchaseListId 进货单ID
     * @return
     */
    @Override
    public Map<String, Object> goodsList(Integer purchaseListId) {
        Map<String, Object> map = new HashMap<>();
        List<PurchaseListGoods> purchaseListGoodsList = purchaseListGoodsDao.getPurchaseListGoodsByPurchaseListId(purchaseListId);
        map.put("rows", purchaseListGoodsList);
        return map;
    }

    /**
     * 删除进货单及商品信息
     * @param purchaseListId 进货单ID
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer purchaseListId) {
        purchaseListGoodsDao.deletePurchaseListGoodsByPurchaseListId(purchaseListId);
        purchaseListDao.deletePurchaseListById(purchaseListId);
    }

    /**
     * 修改进货单付款状态
     * @param purchaseListId 进货单ID
     * @return
     */
    @Override
    public void updateState(Integer purchaseListId) {
        purchaseListDao.updateState(purchaseListId);
    }

    /**
     * 进货商品统计
     * @param sTime 开始时间
     * @param eTime 结束时间
     * @param goodsTypeId 商品类别ID
     * @param codeOrName 编号或商品名称
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        JsonArray result = new JsonArray();
        List<PurchaseList> purchaseListList = purchaseListDao.getPurchaselist(sTime, eTime);
        for (PurchaseList purchaseList : purchaseListList) {
            List<PurchaseListGoods> purchaseListGoodsList = purchaseListGoodsDao.getPurchaseListGoods(purchaseList.getPurchaseListId(), goodsTypeId, codeOrName);
            for (PurchaseListGoods purchaseListGoods : purchaseListGoodsList) {
                JsonObject obj = new JsonObject();

                obj.addProperty("number", purchaseList.getPurchaseNumber());

                obj.addProperty("date", purchaseList.getPurchaseDate());

                obj.addProperty("supplierName", purchaseList.getSupplierName());

                obj.addProperty("code", purchaseListGoods.getGoodsCode());

                obj.addProperty("name", purchaseListGoods.getGoodsName());

                obj.addProperty("model", purchaseListGoods.getGoodsModel());

                obj.addProperty("goodsType", goodsTypeDao.getGoodsTypeById(purchaseListGoods.getGoodsTypeId()).getGoodsTypeName());

                obj.addProperty("unit", purchaseListGoods.getGoodsUnit());

                obj.addProperty("price", purchaseListGoods.getPrice());

                obj.addProperty("num", purchaseListGoods.getGoodsNum());

                obj.addProperty("total", purchaseListGoods.getTotal());

                result.add(obj);
            }
        }

        return result.toString();
    }
/*


    @Override
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {

        JsonArray result = new JsonArray();

        try {

            List<PurchaseList> purchaseListList = purchaseListGoodsDao.getPurchaselist(null, null, null, sTime, eTime);

            for(PurchaseList pl : purchaseListList){

                List<PurchaseListGoods> purchaseListGoodsList = purchaseListGoodsDao
                        .getPurchaseListGoods(pl.getPurchaseListId(), goodsTypeId, codeOrName);

                for(PurchaseListGoods pg : purchaseListGoodsList){

                    JsonObject obj = new JsonObject();

                    obj.addProperty("number", pl.getPurchaseNumber());

                    obj.addProperty("date", pl.getPurchaseDate());

                    obj.addProperty("supplierName", pl.getSupplierName());

                    obj.addProperty("code", pg.getGoodsCode());

                    obj.addProperty("name", pg.getGoodsName());

                    obj.addProperty("model", pg.getGoodsModel());

                    obj.addProperty("goodsType", goodsTypeDao.getGoodsTypeById(pg.getGoodsTypeId()).getGoodsTypeName());

                    obj.addProperty("unit", pg.getGoodsUnit());

                    obj.addProperty("price", pg.getPrice());

                    obj.addProperty("num", pg.getGoodsNum());

                    obj.addProperty("total", pg.getTotal());

                    result.add(obj);

                }
            }

            logService.save(new Log(Log.SELECT_ACTION, "进货商品统计查询"));

        } catch (Exception e) {

            e.printStackTrace();

        }

        return result.toString();
    }*/
}
